<?php
namespace app\pic\admin;
use app\system\admin\Admin;
use think\Db;
class Pic extends Admin
{
    protected $hisiModel = '';//模型名称[通用添加、修改专用]
    protected $hisiTable = 'pic';//表名称[通用添加、修改专用]
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名
    protected function initialize()
    {
        parent::initialize();
        $authors = Db::name('users')->where('user_type',1)->select();
        $this->assign('authors', $authors);
        $cates = Db::name('pic_cate')->where('status',1)->where('id','<>',5)->order('sort asc')->select();
        $this->assign('cates', $cates);
    }
    public function index() {
        if($this->request->isAjax()){
            $where = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $title      = $this->request->param('title', '');
            $author_id  = $this->request->param('author_id/d', '');
            $cate_id    = $this->request->param('cate_id/d', '');
            if(!empty($title)){
                $where[] = ['title','like','%'.$title.'%'];
            }
            if(!empty($author_id)){
                $where[] = ['author_id','eq',$author_id];
            }
            if(!empty($cate_id)){
                $where[] = ['cate_id','eq',$cate_id];
            }
            $model = Db::name('pic')->alias('p');

            $field = 'p.*,pc.name as cate_name,u.nickname';
            $data['data'] = $model->field($field)
            ->join('pic_cate pc','pc.id=p.cate_id')
            ->join('users u','u.id=p.author_id')
            ->where($where)
            ->page($page)
            ->limit($limit)
            ->order('p.sort acs,p.id desc')
            ->select();
            $data['count'] = $model->where($where)->count('p.id');
            $data['code'] = 0;
            $data['msg'] = '';
            return json($data);
        }

		return $this->fetch('index');
    }
    
    public function vote()
    {
    	if($this->request->isAjax()){
            $where = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $title      = $this->request->param('title', '');
            $author_id  = $this->request->param('author_id/d', '');
            $cate_id    = 4;
            if(!empty($title)){
                $where[] = ['title','like','%'.$title.'%'];
            }
            if(!empty($author_id)){
                $where[] = ['author_id','eq',$author_id];
            }
            if(!empty($cate_id)){
                $where[] = ['cate_id','eq',$cate_id];
            }
            $model = Db::name('pic')->alias('p');

            $field = 'p.*,pc.name as cate_name,u.nickname,count(v.id) as vote';
            $data['data'] = $model->field($field)
            ->join('pic_cate pc','pc.id=p.cate_id','left')
            ->join('users u','u.id=p.author_id','left')
            ->join('vote_log v', 'v.pic_id=p.id','left')
            ->where($where)
            ->page($page)
            ->limit($limit)
            ->group('p.id')
            ->order('vote desc')
            ->select();
            // foreach ($data['data'] as $key=>$v){
            // 	$data['data'][$key]['vote'] = Db::name('vote_log')->where('pic_id',$v['id'])->count('id');
            // }
            // var_dump($model->getlastsql());
            // halt($data['data']);
            $data['count'] = $model->where($where)->count('p.id');
            $data['code'] = 0;
            $data['msg'] = '';
            return json($data);
        }

		return $this->fetch('index');
    }
}
